<template>
  <el-dialog
    v-if="visible"
    :title="info.title"
    :visible.sync="visible"
    append-to-body
    center
    width="760px"
    :before-close="handleCancel"
  >
    <el-form
      ref="formDate"
      label-suffix=":"
      label-width="120px"
      :rules="rules"
      :model="form"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="外包合同" prop="pur">
            <PurchaseSelectObject v-model="form.pur" :id="form.pur_id" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="项目名称">
            <span v-if="purchase.id">{{ purchase.pro_name }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="期别" prop="stage">
            <el-input v-model="form.stage" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="PO单号">
            <span v-if="purchase.id">{{ 'PO' + purchase.pro_id }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用途" prop="purpose">
            <el-input v-model="form.purpose" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="供应商">
            <span v-if="purchase.id">{{ purchase.sup_name }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="本期付款" prop="payment">
            <el-input v-model="form.payment" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            style="height: 37px; overflow: hidden"
            label="合同总金额"
          >
            <span v-if="purchase.id">{{
              purchase.price + purchase.additional - purchase.reduction
            }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="本期扣款" prop="deduction">
            <el-input v-model="form.deduction" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="已付款">
            <span>{{ voucher.total }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="小包进度" prop="speed">
            <el-input v-model="form.speed" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="累计付款">
            <span>{{ allPayment() }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="付款方式" prop="type">
            <el-select v-model="form.type" size="small" style="width: 100%">
              <el-option
                v-for="(item, index) in types"
                :key="index"
                :label="item.value"
                :value="index"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="余额">
            <span>{{ yue() }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="付款条件" prop="condition">
            <el-input
              v-model="form.condition"
              size="small"
              clearable
              style="width: 90%"
            />
            %
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="付款依据" prop="basis">
            <el-select v-model="form.basis" size="small" style="width: 100%">
              <el-option
                v-for="item in basiss"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="付款周期" prop="cycle">
            <el-input
              v-model="form.cycle"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="附件上传" prop="files">
            <Upload v-model="form.files" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              :rows="4"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col class="mt10" :span="24">
          <el-form-item label="抄送" prop="ccs">
            <VerifyCC v-model="form.ccs" :ccs="ccs" />
          </el-form-item>
        </el-col> -->
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleBeforeTo()"
              >提 交</el-button
            >
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import PurchaseSelectObject from '@/myComponents/PurchaseSelectObject'
// import VerifyCC from '@/myComponents/VerifyCC'
import Upload from '@/myComponents/Upload'
import svf from '../../util/svf'
import { payType } from '@/api/project'
import { pur as queryPur } from '@/api/purchase'
import { sumAmount } from '@/api/finance/expend'

export default {
  components: { PurchaseSelectObject, Upload },
  extends: svf,
  data() {
    const vnull = (rule, value, callback) => {
      if (JSON.stringify(value) === '{}') {
        callback(new Error(rule.message))
      } else {
        callback()
      }
    }
    return {
      approve_key: 'PROCESS_PRO_OUTSOURCE_EXPEND',
      types: [],
      voucher: {},
      purchase: {},
      rules: {
        pur: [
          {
            required: true,
            trigger: 'blur',
            message: '外包合同不可为空',
            validator: vnull,
          },
        ],
        stage: [{ required: true, trigger: 'blur', message: '期别不可为空' }],
        purpose: [{ required: true, trigger: 'blur', message: '用途不可为空' }],
        payment: [
          { required: true, trigger: 'blur', message: '本期付款不可为空' },
        ],
        deduction: [
          { required: true, trigger: 'blur', message: '本期扣款不可为空' },
        ],
        speed: [
          { required: true, trigger: 'blur', message: '小包进度不可为空' },
        ],
        type: [
          { required: true, trigger: 'blur', message: '付款方式不可为空' },
        ],
        basis: [
          { required: true, trigger: 'blur', message: '付款依据不可为空' },
        ],
        condition: [
          { required: true, trigger: 'blur', message: '付款条件不可为空' },
        ],
        cycle: [
          { required: true, trigger: 'blur', message: '付款周期不可为空' },
        ],
        files: [{ required: true, trigger: 'blur', message: '附件不可为空' }],
      },
    }
  },
  watch: {
    visible(val) {
      if (val) {
        if (this.info.type && this.info.type === 'again') {
          this.initApply(this.info.edit)
        } else {
          this.form = this.info.edit
        }
        this.$set(this.form, 'pur', {})
        this.$set(this, 'voucher', {})
        this.$set(this, 'purchase', {})
        this.payType()
        // this.queryCcs()
      }
    },
    'form.pur': {
      handler(n, o) {
        if (this.form.pur.key) {
          this.queryPur()
        }
      },
      deep: true,
    },
  },
  mounted() {},
  methods: {
    payType() {
      payType()
        .then((response) => {
          this.types = response.info
        })
        .catch((err) => {
          this.$message.error(err.msg)
        })
    },
    queryPur() {
      queryPur({ id: this.form.pur.key })
        .then((response) => {
          this.purchase = response.info
          this.sumAmount()
        })
        .catch((err) => {
          this.$message.error(err.msg)
        })
    },
    allPayment() {
      const payment = this.form.payment ? this.form.payment : 0
      const deduction = this.form.deduction ? this.form.deduction : 0
      const total = this.voucher.total ? this.voucher.total : 0
      return payment - deduction + total
    },
    yue() {
      const money =
        (this.purchase.price ? this.purchase.price : 0) +
        this.purchase.additional -
        this.purchase.reduction
      const payment = this.form.payment ? this.form.payment : 0
      const deduction = this.form.deduction ? this.form.deduction : 0
      const total = this.voucher.total ? this.voucher.total : 0
      return money - (payment - deduction + total)
    },
    sumAmount() {
      sumAmount({ pur_id: this.form.pur.key })
        .then((response) => {
          const payment = response.info.payment ? response.info.payment : 0
          const deduction = response.info.deduction
            ? response.info.deduction
            : 0
          const total = payment - deduction
          this.$set(this, 'voucher', { total: total })
        })
        .catch((err) => {
          this.$message.error(err.msg)
        })
    },
    handleBeforeTo() {
      this.validate().then(() => {
        const e = {}
        e.files = this.form.files
        e.approve_key = this.approve_key
        e.des =
          '外包合同：' + this.form.pur.value + '    备注：' + this.form.remark
        e.ccs = this.form.ccs
        e.forms = JSON.stringify(
          this.createForms([
            ['PO单号', 'PO' + this.purchase.pro_id, '', ''],
            ['项目名称', this.purchase.pro_name, '', ''],
            ['供应商', this.purchase.sup_name, '', ''],
            ['外包合同', this.form.pur.value, this.form.pur.key, 'pur_id'],
            ['本期付款', this.form.payment, this.form.payment, 'payment'],
            [
              '合同总金额',
              this.purchase.price +
                this.purchase.additional -
                this.purchase.reduction,
              '',
              '',
            ],
            ['本期扣款', this.form.deduction, this.form.deduction, 'deduction'],
            ['小包进度', this.form.speed, this.form.speed, 'speed'],
            ['付款条件', this.form.condition, this.form.condition, 'condition'],
            ['付款依据', this.form.basis, this.form.basis, 'basis'],
            [
              '付款方式',
              this.types[this.form.type]['value'],
              this.types[this.form.type]['key'],
              'type',
            ],
            ['付款周期', this.form.cycle, this.form.cycle, 'cycle'],
            ['期别', this.form.stage, this.form.stage, 'stage'],
            ['用途', this.form.purpose, this.form.purpose, 'purpose'],
            ['备注', this.form.remark, this.form.remark, 'remark'],
          ])
        )
        this.handleCreate(e)
      })
    },
  },
}
</script>

<style></style>
